package com.d3works.apps.production.web;

import com.d3works.apps.base.db.Product;
import com.d3works.apps.base.db.repo.ProductRepository;
import com.d3works.apps.production.db.TempBomTree;
import com.d3works.apps.production.service.BillOfMaterialService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;

public class ProductController {

  public void openProductTree(ActionRequest request, ActionResponse response) {

    Product product = request.getContext().asType(Product.class);
    product = Beans.get(ProductRepository.class).find(product.getId());

    TempBomTree tempBomTree =
        Beans.get(BillOfMaterialService.class)
            .generateTree(product.getDefaultBillOfMaterial(), true);

    response.setView(
        ActionView.define(I18n.get("Bill of materials"))
            .model(TempBomTree.class.getName())
            .add("tree", "bill-of-material-tree")
            .context("_tempBomTreeId", tempBomTree.getId())
            .map());
  }
}
